<?php
/**
 * 分类管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-17
 * Time: 下午2:37
 */
namespace app\modules\cn\controllers;

use app\libs\IeltsController;
use app\libs\Method;
use app\modules\cn\models\CaseList;
use app\modules\cn\models\Info;
use app\modules\content\models\Active;
use app\modules\content\models\Banner;
use app\modules\content\models\Classes;
use app\modules\content\models\Recommend;

class NewsController extends IeltsController
{
    public $enableCsrfValidation = false;
    public $layout = 'cn';


    public function actionIndex()
    {
        $cate = \Yii::$app->request->get('cate', 'info');//123都是资讯 4是公开课
        $page = \Yii::$app->request->get('page', 1);
        $pageSize = \Yii::$app->request->get('pageSize', 8);
        $offset = $pageSize * ($page - 1);
        if ($cate == 'info') {
            $cate = 1;
            $this->title = '申友雅思在线_雅思最新资讯_机经回忆_雅思备考';
            $this->keywords = '雅思资讯,雅思备考,机经回忆,雅思考试';
            $this->description = '最新的雅思资讯，满足考生各阶段备考需求，机经回忆，雅思考试攻略，只为雅思提分，申友雅思在线';
        } else if ($cate == 'share') {
            $cate = 2;
            $this->title = '申友雅思在线_雅思干货分享_备考攻略';
            $this->keywords = '雅思听力,雅思口语,雅思阅读,雅思写作';
            $this->description = '最全的雅思备考技巧，雅思听力、口语、阅读、写作及雅思核心词汇，满足考生各阶段备考需求，只为雅思提分，申友雅思在线';
        } else if ($cate == 'shenyou') {
            $cate = 3;
            $this->title = '申友雅思在线_申友头条';
            $this->keywords = '申友雅思,雅思名师,雅思奖学金';
            $this->description = '申友雅思为考生提供雅思考试培训，有科学完善的备考体系，满足考生各阶段备考需求，只为雅思提分，申友雅思在线';
        } else if ($cate == 'loc_info') {
            $cate = 4;
            $this->title = '申友雅思在线_雅思考位资讯_考点信息';
            $this->keywords = '雅思考位资讯,最新考点信息,考位指南';
            $this->description = '最新的雅思考位资讯，考点信息指南，帮助考生了解最新的考位信息，满足考生各阶段备考需求，只为雅思提分，申友雅思在线';
        } else {
            return $this->redirect('/404.html');
        }
        // else {
        //     $cate = 1;
        //     $this->title = '申友雅思在线_最新资讯_干货分享_申友头条_考位资讯';
        //     $this->keywords = '雅思资讯,雅思干货分享,雅思备考,雅思考位资讯';
        //     $this->description = '最新的雅思资讯及备考技巧，申友雅思在线为考生提供最新资讯，干货分享，雅考位资讯，满足考生各阶段备考需求，只为雅思提分，申友雅思在线';
        // }
        $time = time();
        $data = Info::find()->asArray()->where("(pushTime<=$time or pushTime is null) and cate=" . $cate)->offset($offset)->limit($pageSize)->orderBy('id desc')->all();
        $count = count(Info::find()->asArray()->where("(pushTime<=$time or pushTime is null) and cate=" . $cate)->all());

        $course = Classes::find()->asArray()->limit(2)->orderBy('viewCount desc')->all();
        $top = Info::find()->asArray()->where('cate=3')->limit(4)->orderBy('viewCount desc')->all();
        $banner = Banner::find()->asArray()->where('cate=4')->limit(4)->orderBy('sort asc,viewCount desc')->all();
        $countPage= $count!=false?ceil($count/$pageSize):0;
        $pageArray = Method::page($countPage, $page);
        return $this->render('index', ['banner'=>$banner,'data' => $data,'pageArray'=>$pageArray,'course'=>$course,'top'=>$top,'pageSize'=>$pageSize]);
    }
    public function actionDetail()
    {
        $id = \Yii::$app->request->get('id');
        $detail = Info::find()->asArray()->where('id='.$id)->one();
        if($detail!=false){
            Info::updateAll(['viewCount' => ($detail['viewCount'] + 1)], "id=$id");
        }
        $recommend = Info::find()->asArray()->orderBy('sort asc,id desc')->limit(10)->all();
        $case= json_decode(file_get_contents("https://ielts.thinkwithu.com/cn/api/thinku-ielts-case?pageSize=8"),true);
        $course = Classes::find()->asArray()->limit(2)->orderBy('viewCount desc')->all();
        $active= Active::find()->asArray()->orderBy('id desc')->limit(2)->all();
        return $this->render('information', ['active'=>$active,'detail'=>$detail,'recommend'=>$recommend,'case'=>$case,'course'=>$course]);
    }
    public function actionInformation()
    {
        $id = \Yii::$app->request->get('id',1);
        $detail = Info::find()->asArray()->where('id='.$id)->one();
        if($detail!=false){
            Info::updateAll(['viewCount' => ($detail['viewCount'] + 1),'count' => ($detail['count'] + 1)], "id=$id");
        }
        $time=time();
        $recommend = Info::find()->asArray()->where('(cate=1 or cate=2) and pushTime<='.$time)->orderBy('sort asc,id desc')->limit(4)->all();
        $active= Active::find()->asArray()->orderBy('id desc')->limit(2)->all();
        $course = Classes::find()->asArray()->limit(3)->orderBy('viewCount desc')->all();
        $this->title = isset($detail['name'])?$detail['name']:'';
        $this->keywords = isset($detail['keywords'])?$detail['keywords']:'';
        $this->description = isset($detail['summary'])?$detail['summary']:'';
        $focus= Recommend::find()->asArray()->where('type=2')->orderBy('sort asc,id desc')->limit(8)->all();
        return $this->render('information', ['detail'=>$detail,'recommend'=>$recommend,'active'=>$active,'course'=>$course,'focus'=>$focus]);
    }
    public function actionHeadline()
    {
        $id = \Yii::$app->request->get('id',1);
        $detail = Info::find()->asArray()->where('id='.$id)->one();
        if($detail!=false){
            Info::updateAll(['viewCount' => ($detail['viewCount'] + 1),'count' => ($detail['count'] + 1)], "id=$id");
        }
        $time=time();
        $recommend = Info::find()->asArray()->where('cate=3 and pushTime<='.$time)->orderBy('viewCount desc')->limit(4)->all();
        $case = CaseList::find()->asArray()->limit(8)->orderBy('id desc')->all();
        $course = Classes::find()->asArray()->limit(2)->orderBy('viewCount desc')->all();
        $this->title = isset($detail['name'])?$detail['name']:'';
        $this->keywords = isset($detail['keywords'])?$detail['keywords']:'';
        $this->description = isset($detail['summary'])?$detail['summary']:'';
        $focus= Recommend::find()->asArray()->where('type=2')->orderBy('sort asc,id desc')->limit(8)->all();
        return $this->render('headline',['detail'=>$detail,'recommend'=>$recommend,'case'=>$case,'course'=>$course,'focus'=>$focus]);
    }

    /**
     * [actionShareActiveDetail 分享的资讯详情]
     * @Author don
     * @Date   2021-12-15
     * @return [type]     [description]
     */
    public function actionShareDetail()
    {
        $id = \Yii::$app->request->get('id');
        $data = Info::find()->where(['id'=>$id])->asArray()->one();
        $data['content'] = str_replace("/files/", "http://www.thinkuprep.com/files/",$data['content']);
        // 更新浏览量
        Info::updateAll(['viewCount' => ($data['viewCount'] + 1)], "id=$id");
        return $this->renderPartial('share', [
            'data' => $data
        ]);
    }

    public function actionAmanda()
    {
        $page = \Yii::$app->request->get('page', 1);
        $pageSize = \Yii::$app->request->get('pageSize', 8);
        $offset = $pageSize * ($page - 1);

        $this->title = '申友雅思在线_雅思考位资讯_考点信息';
        $this->keywords = '雅思考位资讯,最新考点信息,考位指南';
        $this->description = '最新的雅思考位资讯，考点信息指南，帮助考生了解最新的考位信息，满足考生各阶段备考需求，只为雅思提分，申友雅思在线';
        $time = time();
        $data = Info::find()->asArray()->where("(pushTime<=$time or pushTime is null) and cate=6" )->offset($offset)->limit($pageSize)->orderBy('id desc')->all();
        $count = count(Info::find()->asArray()->where("(pushTime<=$time or pushTime is null) and cate=6" )->all());
        $course = Classes::find()->asArray()->limit(2)->orderBy('viewCount desc')->all();
        $top = Info::find()->asArray()->where('cate=3')->limit(4)->orderBy('viewCount desc')->all();
        $banner = Banner::find()->asArray()->where('cate=4')->limit(4)->orderBy('sort asc,viewCount desc')->all();
        $countPage = $count != false ? ceil($count / $pageSize) : 0;
        $pageArray = Method::page($countPage, $page);
        return $this->render('amanda', ['banner' => $banner, 'data' => $data, 'pageArray' => $pageArray, 'course' => $course, 'top' => $top, 'pageSize' => $pageSize]);
    }
}